package com.abe.libcore.utils.normal;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;

import com.abe.libcore.utils.Utils;

import java.util.Date;

public class CommonUtils {
    public static void sendBroadcast(String action) {
        sendBroadcast(action, new Bundle());
    }

    public static void sendLocalBroadcast(String action) {
        sendLocalBroadcast(action, new Bundle());
    }

    public static void sendBroadcast(String action, Bundle bundle) {
        Intent callIntent = new Intent();
        callIntent.putExtras(bundle);
        callIntent.setAction(action);
        Utils.getContext().sendBroadcast(callIntent);
    }

    public static void sendLocalBroadcast(String action, Bundle bundle) {
        Intent callIntent = new Intent();
        callIntent.putExtras(bundle);
        callIntent.setAction(action);
        LocalBroadcastManager.getInstance(Utils.getContext()).sendBroadcast(callIntent);
    }

    public static void w(String message) {
        boolean x = CommonUtils.getMetaBooleanData(Utils.getContext(), "LogDebug");
        if (!x) return;
        System.out.println(message);
    }

    public static int getColor(int colorRes) {
        return Utils.getContext().getResources().getColor(colorRes);
    }

    public static Boolean getMetaBooleanData(Context context, String dataName) {
        Boolean msg = false;
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            msg = appInfo.metaData.getBoolean(dataName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static int getMetaIntData(Context context, String dataName) {
        int msg = 0;
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            msg = appInfo.metaData.getInt(dataName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static String getMetaStringData(Context context, String dataName) {
        String msg = "";
        try {
            ApplicationInfo appInfo = context.getPackageManager()
                    .getApplicationInfo(context.getPackageName(),
                            PackageManager.GET_META_DATA);
            msg = appInfo.metaData.getString(dataName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return msg;
    }

    public static void saveLocalLog(String title, String message, String path) {
        LocalLogUtils.saveLocalLog(title, message, path);
    }

    public static void saveLocalLog(String title, String message) {
        saveLocalLog(title, message, ConstantCore.LOG_PATH);
    }

    public static void saveLocalDailyLog(Date date, String message, String path) {
        LocalLogUtils.saveLocalDailyLog(date, message, path);
    }

    public static void saveLocalDailyLog(String message, String path) {
        saveLocalDailyLog(new Date(), message, path);
    }
}